home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 16 / AMIGAplus Sonderheft 16 (1998)(ICP)(DE)[!].iso / pd / anwendungen / dcf77 / developer / waitdcf77ifcrash.c < prev    next >
C/C++ Source or Header  |  1998-08-01  |  4KB  |  169 lines

  1. /* WaitDCF77ifCrash                                                 ***RGR***
  2.  
  3.    Author:   Ralf Gruner, An der Sense 5a, D-02779 Großschönau, Germany.
  4. */
  5.  
  6. #include <exec/types.h>
  7. #include <exec/semaphores.h>
  8. #include <clib/exec_protos.h>
  9. #include <clib/dos_protos.h>
  10. #include <stdio.h>
  11.  
  12. BPTR    DateFile =NULL;
  13. char *filename1    ="ENVARC:LastBootDate";
  14. BPTR    CONWin =NULL;
  15. char *filename2    ="CON:20/20/280/80/WaitDCF77ifCrash/AUTO";
  16.  
  17. char *version="$VER: WaitDCF77ifCrash 1.5 (27.7.98)";
  18.  
  19.  
  20. struct StateList
  21. {
  22.     struct SignalSemaphore sl_Semaphore;
  23.     STRPTR Version;
  24.     BOOL Received;    // hier können andere Tasks nachschauen, ob die Systemzeit gestellt ist.
  25.     LONG ReceiverState; // 0 - kein Signal. 1 -Empfang gestört.
  26.                               // 2 - Warte auf Synchronisation. 3 - Empfang läuft.
  27.                               // 4 - Programm inaktiv
  28.     LONG TimeLeft; // voraussichtl. Dauer [s] bis zum nächsten Stellen der Zeit
  29.     LONG LastError;
  30. };
  31.  
  32. #define RCVSTATE_NOSIGNAL        0
  33. #define RCVSTATE_BADSIGNAL        1
  34. #define RCVSTATE_WAITFORSYNC    2
  35. #define RCVSTATE_RECEIVING        3
  36. #define RCVSTATE_INACTIVE        4
  37.  
  38. struct StateList *DCF77PublicState=NULL, CopyOfState;
  39.  
  40. UBYTE    * DCF77SemaphoreName = "DCF77 State";
  41. BOOL    DCF77received=FALSE;
  42.  
  43. main(int argc, char *argv[])
  44. {
  45.     struct DateStamp DS, DSfromFile;
  46.     long bytes;
  47.     LONG DCFVersion=0, DCFRevision=0, Characters;
  48.     char *msg1="";
  49.     char *msg2=    " © 1994-1998 Ralf Gruner, Großschönau, Germany.\n\n"
  50.                     " WaitDCF77ifCrash waits for received time from DCF77\n if the date of your hardware clock has been corrupted.\n"
  51.                     " Usage: Place it in a script above your date sensitive application.\n\n";
  52.  
  53.     if(argc != 1)
  54.     {
  55.         puts(msg1);
  56.         puts(version+5);
  57.         puts(msg2);
  58.     }
  59.     else
  60.     {
  61.         DateStamp((struct DateStamp *)&DS);    // Systemzeit in Struktur eintragen
  62.         
  63.         if (NULL!=(DateFile = Open(filename1,MODE_OLDFILE)))
  64.         {
  65.             bytes = Read(DateFile,&DSfromFile,(LONG)sizeof(struct DateStamp));
  66.             Close(DateFile);
  67.     
  68.             if (bytes != (LONG)sizeof(struct DateStamp))
  69.             {
  70.                 DSfromFile.ds_Days=0;
  71.             }
  72.     
  73.         }
  74.         else
  75.             DSfromFile.ds_Days=0;
  76.  
  77.         if(DSfromFile.ds_Days > DS.ds_Days || DSfromFile.ds_Days < DS.ds_Days-182)
  78.         {
  79.             Delay(500L);    // ggf. auf Workbench-Window warten
  80.             CONWin = Open(filename2,MODE_NEWFILE);
  81.  
  82.             while(!DCF77received)
  83.             {
  84.                 Forbid();
  85.                 if(DCF77PublicState=(struct StateList *) FindSemaphore(DCF77SemaphoreName))
  86.                 {
  87.                     ObtainSemaphoreShared((struct SignalSemaphore *) DCF77PublicState);
  88.                     DCF77received=DCF77PublicState->Received;
  89.  
  90.                     Characters=StrToLong(DCF77PublicState->Version,&DCFVersion);
  91.                     StrToLong(DCF77PublicState->Version+Characters+1,&DCFRevision);
  92.                     if(DCFVersion>2 || DCFVersion==2 && DCFRevision>=18)
  93.                     {
  94.                         CopyOfState=*DCF77PublicState;
  95.                     }
  96.                     else
  97.                     {
  98.                         CopyOfState.ReceiverState=-2; // alte DCF77-Version
  99.                     }
  100.  
  101.                     ReleaseSemaphore((struct SignalSemaphore *) DCF77PublicState);
  102.                 }
  103.                 else
  104.                 {
  105.                     DCF77received=FALSE;    // DCF77 läuft nicht
  106.                     CopyOfState.ReceiverState=-1;
  107.                 }
  108.                 Permit();
  109.     
  110.                 if(CONWin)
  111.                 {
  112.                     FPrintf(CONWin,"\2330 p\2330;0H\n\n Waiting for DCF77...\n\n\233K");
  113.                     if(!DCF77received)
  114.                     {
  115.                         switch(CopyOfState.ReceiverState)
  116.                         {
  117.                             case -1:
  118.                                 FPrintf(CONWin," DCF77 is not running.\n");
  119.                                 break;
  120.                             case RCVSTATE_NOSIGNAL:
  121.                                 FPrintf(CONWin," No signal.\n");
  122.                                 break;
  123.                             case RCVSTATE_BADSIGNAL:
  124.                                 FPrintf(CONWin," Bad signal.\n");
  125.                                 break;
  126.                             case RCVSTATE_WAITFORSYNC:
  127.                                 FPrintf(CONWin," Waiting for synchronisation.\n");
  128.                                 break;
  129.                             case RCVSTATE_RECEIVING:
  130.                                 FPrintf(CONWin," Receiving. %3ld left.\n",CopyOfState.TimeLeft);
  131.                                 break;
  132.                             case RCVSTATE_INACTIVE:
  133.                                 FPrintf(CONWin," Inactive.\n");
  134.                                 break;
  135.                         }
  136.                     }
  137.                 }
  138.  
  139.                 if(!DCF77received)
  140.                     Delay(100L);    // zwei Sekunden warten und dann erneut testen
  141.             }
  142.  
  143.             DateStamp((struct DateStamp *)&DS);    // neue Systemzeit in Struktur eintragen
  144.  
  145.             if(CONWin)
  146.             {
  147.                 FPrintf(CONWin," Time is okay now.\n");
  148.                 Delay(150L);
  149.                 Close(CONWin);
  150.             }
  151.         }
  152.  
  153.         if(DSfromFile.ds_Days != DS.ds_Days)
  154.         {
  155.             // nur speichern, wenn Tag geändert
  156.             if (NULL!=(DateFile = Open(filename1,MODE_NEWFILE)))
  157.             {
  158.                 bytes = Write(DateFile,&DS,(LONG)sizeof(struct DateStamp));
  159.                 if (bytes != (LONG)sizeof(struct DateStamp))
  160.                 {
  161.                     puts("Error while writing");
  162.                 }
  163.                 Close(DateFile);
  164.             }
  165.         }
  166.     }
  167. }
  168.  
  169.